package com.ankiang.treeprint.fragment.home;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.ankiang.treeprint.R;
import com.ankiang.treeprint.fragment.util.TRPages;
import com.ankiang.treeprint.fragment.util.TRUtils;
import com.ankiang.treeprint.manager.QDDataManager;
import com.ankiang.treeprint.view.TRConfig;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView;

import org.devio.takephoto.app.TakePhotoFragment;

import butterknife.BindView;

/**
 * @author cginechen
 * @date 2016-10-20
 */

public class HomeMeController extends HomeController {


    String callname = "我";
    int id = 12345678;
    char sex = 'M';
    String remark = "家国天下";
    String nickname = "小吴";
    String name = "吴京";


    @BindView(R.id.groupListView)
    QMUIGroupListView mGroupListView;

    // 昵称
    @BindView(R.id.item_nickname)
    TextView itemNicknameTextView;

    // 称呼
    @BindView(R.id.item_callname)
    TextView itemCallNameTextView;

    // 称呼
    @BindView(R.id.lucky_number)
    TextView luckyNumberTextView;

    // 姓名
    @BindView(R.id.item_name)
    TextView itemNameTextView;

    @BindView(R.id.item_id)
    TextView itemIdTextView;

    @BindView(R.id.item_sex)
    ImageButton itemSexImageButton;


    @BindView(R.id.item_image)
    ImageButton itemImageButton;

    @BindView(R.id.item_remark)
    TextView itemRemarkTextView;


    // 配置
    private TRConfig conf;

    public HomeMeController(Context context) {
        super(context, R.layout.home_me_layout);
        initRecyclerView();
        conf = TRConfig.getInstance();

        initGroupListView();
        initListener();
    }

    @Override
    protected String getTitle() {
        return V(R.string.title_me);
    }

    @Override
    protected ItemAdapter getItemAdapter() {
        return new ItemAdapter(getContext(), QDDataManager.getInstance(getContext()).getMeDescriptions());
    }


    private void initListener() {
        itemSexImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSexChoiceDialog();
            }
        });

        itemNicknameTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showEditNicknameDialog();
            }
        });
        itemNameTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showEditNameDialog();
            }
        });
        itemRemarkTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showEditRemarkDialog();
            }
        });
        itemCallNameTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showEditCallnameDialog();
            }
        });
        itemImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showEditImageDialog();
            }
        });

        luckyNumberTextView.setOnClickListener((v)->{
            showEditLuckyNumber();
        });
    }




    private void initGroupListView() {

        itemCallNameTextView.setText(callname);
        itemIdTextView.setText(String.format(V(R.string.details_id_format), id));

        // 性别
        itemSexImageButton.setImageResource(conf.getSexImgId(sex));

        itemRemarkTextView.setText(remark);

        itemNicknameTextView.setText(nickname);
        itemNameTextView.setText(name);

        // todo itemImageButton not set yet




        // 添加功能
        QMUIGroupListView.Section section = QMUIGroupListView.newSection(getContext());
        TRUtils.addFunctionItem(section, mGroupListView, V(R.string.me_func_setting),R.drawable.ic_icon_min_settings,
                (v) -> TRPages.showSettingsPage(HomeMeController.this));
        TRUtils.addFunctionItem(section, mGroupListView, V(R.string.me_func_help),R.drawable.ic_icon_min_help,
                (v) -> TRPages.showHelpPage(HomeMeController.this));
        section.addTo(mGroupListView);


        section = QMUIGroupListView.newSection(getContext());
        TRUtils.addFunctionItem(section, mGroupListView, V(R.string.me_func_about),R.drawable.ic_icon_min_about,
                (v) -> TRPages.showAboutPage(HomeMeController.this));
        TRUtils.addFunctionItem(section, mGroupListView, V(R.string.me_func_app_version),R.drawable.ic_icon_min_app_version,
                (v) -> TRPages.showAppVersionPage(HomeMeController.this));
        section.addTo(mGroupListView);


        section = QMUIGroupListView.newSection(getContext());
        TRUtils.addFunctionItem(section, mGroupListView, V(R.string.me_func_support),R.drawable.ic_icon_min_support,
                (v) -> TRPages.showSupportPage(HomeMeController.this));
        TRUtils.addFunctionItem(section, mGroupListView, V(R.string.me_func_biz_contact),R.drawable.ic_icon_min_biz_contact,
                (v) -> TRPages.showBizContactPage(HomeMeController.this));
        section.addTo(mGroupListView);
    }


    private void showSexChoiceDialog() {
        final String[] items = new String[]{
                V(R.string.item_sex_male),
                V(R.string.item_sex_female),
                V(R.string.item_sex_couple),
                V(R.string.item_sex_neuter),
                V(R.string.item_sex_unknown)};
        final char[] sexChars = new char[]{
                'M', 'F', '0', '1', '?'
        };

        int checkedIndex = 4;
        for (int i = 0; i < sexChars.length; i++) {
            if (sexChars[i] == sex) {
                checkedIndex = i;
                break;
            }
        }
        new QMUIDialog.CheckableDialogBuilder(getActivity())
                .setCheckedIndex(checkedIndex)
                .addItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getActivity(), "你选择了 " + items[which], Toast.LENGTH_SHORT).show();
                        char ch = sexChars[which];
                        sex = ch;
                        itemSexImageButton.setImageResource(conf.getSexImgId(ch));
                        dialog.dismiss();
                    }
                })
                .create().show();
    }


    private void showEditLuckyNumber() {
        final QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(getActivity());
        builder.setTitle(V(R.string.additem_order))
                .setPlaceholder("输入幸运数字，只能设置一次")
                .setInputType(InputType.TYPE_CLASS_NUMBER)
                .addAction(V(R.string.cancel), new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction(V(R.string.ok), new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        CharSequence text = builder.getEditText().getText();
                        if (text != null && text.length() > 0) {
                            int x = Integer.parseInt(String.valueOf(text));
                            luckyNumberTextView.setText(String.valueOf(x));
                            dialog.dismiss();
                        } else {
                            Toast.makeText(getActivity(), V(R.string.additem_enter_content_tips), Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .create().show();
    }

    private void showEditNameDialog() {
        final QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(getActivity());
        builder.setTitle(V(R.string.additem_name))
                .setPlaceholder(V(R.string.additem_enter_content_tips))
                .setInputType(InputType.TYPE_CLASS_TEXT)
                .addAction(V(R.string.cancel), new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction(V(R.string.ok), new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        CharSequence text = builder.getEditText().getText();
                        if (text != null && text.length() > 0) {
                            name = text.toString();
                            itemNameTextView.setText(text);
                            dialog.dismiss();
                        } else {
                            Toast.makeText(getActivity(), V(R.string.additem_enter_content_tips), Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .create().show();
    }

    private void showEditNicknameDialog() {
        final QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(getActivity());
        builder.setTitle(V(R.string.additem_nickname))
                .setPlaceholder(V(R.string.additem_enter_content_tips))
                .setInputType(InputType.TYPE_CLASS_TEXT)
                .addAction(V(R.string.cancel), new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction(V(R.string.ok), new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        CharSequence text = builder.getEditText().getText();
                        if (text != null && text.length() > 0) {
                            nickname = text.toString();
                            itemNicknameTextView.setText(text);
                            dialog.dismiss();
                        } else {
                            Toast.makeText(getActivity(), V(R.string.additem_enter_content_tips), Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .create().show();
    }

    private void showEditRemarkDialog() {
        final QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(getActivity());
        builder.setTitle(V(R.string.additem_remark))
                .setPlaceholder(V(R.string.additem_enter_content_tips))
                .setInputType(InputType.TYPE_CLASS_TEXT)
                .addAction(V(R.string.cancel), new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction(V(R.string.ok), new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        CharSequence text = builder.getEditText().getText();
                        if (text != null && text.length() > 0) {
                            remark = text.toString();
                            itemRemarkTextView.setText(text);
                            dialog.dismiss();
                        } else {
                            Toast.makeText(getActivity(), V(R.string.additem_enter_content_tips), Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .create().show();
    }

    private void showEditCallnameDialog() {
        final QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(getActivity());
        builder.setTitle(V(R.string.additem_callname))
                .setPlaceholder(V(R.string.additem_enter_content_tips))
                .setInputType(InputType.TYPE_CLASS_TEXT)
                .addAction(V(R.string.cancel), new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction(V(R.string.ok), new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        CharSequence text = builder.getEditText().getText();
                        if (text != null && text.length() > 0) {
                            callname = text.toString();
                            itemCallNameTextView.setText(text);
                            dialog.dismiss();
                        } else {
                            Toast.makeText(getActivity(), V(R.string.additem_enter_content_tips), Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .create().show();
    }


    private void showEditImageDialog() {


//        Intent openAlbumIntent = new Intent(
//                Intent.ACTION_GET_CONTENT);
//        openAlbumIntent.setType("image/*");
//        //用startActivityForResult方法，待会儿重写onActivityResult()方法，拿到图片做裁剪操作
//
//        startActivityForResult(openAlbumIntent, CHOOSE_PICTURE);




//        TRImageChooserFragment chooser = new TRImageChooserFragment();
//        chooser.setmImage(itemImageButton);
//        this.startFragment(chooser);

        TakePhotoFragment fragment = new TakePhotoFragment();
        fragment.setImageButton(itemImageButton);

        this.startFragment(fragment);



//        this.startFragment(chooser);


//        chooser.showChoosePicDialog(getActivity());


//        chooser.onAttach(getContext());
//
    }

    private Context getActivity() {
        return this.getContext();
    }



}
